package com.qyer.util.log.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedHashMap;
import java.util.Map;

public class CateUtil {
	
	public static String getCate(String url , Map<String , String>cateMap) {
		
		String res= getCateByUrl( url , cateMap);
		
		return res; 
	}
	
	public static String getCateByUrl(String url , Map<String , String>cateMap) {
		String res = null;
		/*****************wangzhen start******************/
		for(String cate : cateMap.keySet()){
			if ( cate.indexOf(",")>-1){
				for(String cs : cate.split(",")){
					if(url.contains(cs)){
						res=cateMap.get(cate); 
						return res; 
					}
				}
			}else{
				if(url.contains(cate)){
					res=cateMap.get(cate); 
					return res; 
				}
			}
			
		}
		/*****************wangzhen stop******************/
		if(res==null){
			//insite_other
			res="23"; 
		}
		 
		return res; 
	}
	
	public static void main(String[] args) {
		String url ="m.qyer.com";
		
		Map<String ,String > cateMap =getCateMap (); 
		
		String cate = getCate(url ,cateMap) ; 
		
		System.out.println(cate);
	}
	
	public static Map<String ,String> getCateMap (){
		Map<String ,String > cateMap = new LinkedHashMap<String ,String  >();
		
		BufferedReader in = null;

		try {
			// 从当前作业中获取要缓存的文件
//			Path[] paths = DistributedCache.getLocalCacheFiles(job);
			
//			System.out.println("paths===="+paths);
			String line = null;
			String[] strArr = null;
			
			File path = new File ("/Users/wangzhen/Documents/cate_business.txt") ;
			
			in = new BufferedReader((new InputStreamReader(new FileInputStream(path),"utf-8")));
			while (null != (line = in.readLine())) {
				strArr = line.split("\t", -1);
				
				if(strArr.length<4){
					continue ;
				}
				
				cateMap.put(strArr[1]+","+strArr[2], strArr[3]);
			}
			return cateMap ;
		} catch (IOException e) {
			e.printStackTrace();
			return null ;
		} finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
